package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BpmnInfoServiceImpl implements IBpmnInfoService {

    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(bpmnInfoMapper.selectForList(qo));
    }

    @Override
    public String uploadBpmnInfo(MultipartFile file) throws IOException {
        //使用File把文件保存到服务器的磁盘当中
        //获取文件的扩展名
        String ext = FileUploadUtils.getExtension(file);
        //判断是否是bpmn结尾的文件,如果不是抛出异常
        if (!"bpmn".equals(ext)) {
            throw new BusinessException("请上传文件");
        }

        //通过工具类把文件保存到服务器的磁盘中
        String path = FileUploadUtils.upload(SystemConfig.getProfile(),file);
        return path;
    }

    @Override
    public void deploy(String path, String info, String bpmnType) throws FileNotFoundException {
        //根据path找到最终文件
        File file = new File(SystemConfig.getProfile() ,path);
        //通过activiti的流程图把流程部署起来
        Deployment deployment = repositoryService.createDeployment()
                .addInputStream(path, new FileInputStream(file))
                .deploy();
        //查询流程定义对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        //把信息最终保存一份到BpmnInfoService中
        BpmnInfo bpmnInfo = new BpmnInfo();
        bpmnInfo.setBpmnType(bpmnType);
        bpmnInfo.setInfo(info);
        bpmnInfo.setDeployTime(deployment.getDeploymentTime());
        bpmnInfo.setDeploymentId(deployment.getId());
        bpmnInfo.setBpmnName(processDefinition.getName());
        bpmnInfo.setActProcessId(processDefinition.getId());
        bpmnInfo.setActProcessKey(processDefinition.getKey());
        bpmnInfoMapper.insert(bpmnInfo);
    }

    @Override
    public void delete(Long id) {
        //通过id查询出来BpmnInfo
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(id);
        //删除bpmnInfo信息
        bpmnInfoMapper.deleteByPrimaryKey(id);
        //删除activiti中的信息
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(bpmnInfo.getDeploymentId()).singleResult();
        repositoryService.deleteDeployment(bpmnInfo.getDeploymentId());
        //删除文件
        File file = new File(SystemConfig.getProfile(), processDefinition.getResourceName());
        if (file.exists()) {
            file.delete();
        }
    }

    @Override
    public InputStream readResource(Long deploymentId, String type) {
        InputStream inputStream = null;
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId.toString()).singleResult();
        if ("xml".equals(type)) {
            inputStream = repositoryService.getResourceAsStream(deploymentId.toString() , processDefinition.getResourceName());
        } else {
            //响应png
            DefaultProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator();
            BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
            /**
             * 第一个参数是我们流程图的节点信息
             * 第二个参数,高量集合节点信息
             * 第二个参数是高亮连线集合
             */
            inputStream = defaultProcessDiagramGenerator.generateDiagram(bpmnModel
                    , Collections.emptyList()
                    , Collections.emptyList()
                    ,"宋体","宋体","宋体"
            );
        }
        return inputStream;
    }

    @Override
    public List<BpmnInfo> queryByType(String car_package) {
        return bpmnInfoMapper.queryByType(car_package);
    }
}
